/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.basics.manager.impl;

import org.apache.commons.betwixt.expression.Context;
import org.apache.commons.betwixt.strategy.TypeBindingStrategy.BindingType;
import org.springframework.util.StringUtils;

import com.genia.toolbox.basics.bean.RegisterableObjectStringConverter;
import com.genia.toolbox.basics.bean.RegisterableTypeBindingStrategy;
import com.genia.toolbox.basics.exception.technical.WrapperException;

/**
 * Xml mapping configuration for classes.
 */
public class ClassXmlMappingConfigurator
    implements RegisterableTypeBindingStrategy, RegisterableObjectStringConverter
{

  /**
   * returns the {@link BindingType} for the handled type.
   * 
   * @return the {@link BindingType} for the handled type
   * @see com.genia.toolbox.basics.bean.RegisterableTypeBindingStrategy#getBindingType()
   */
  public BindingType getBindingType()
  {
    return BindingType.PRIMITIVE;
  }



  /**
   * returns the type of data that this configurator handle.
   * 
   * @return the type of data that this configurator handle
   * @see com.genia.toolbox.basics.bean.RegisterableTypeBindingStrategy#getType()
   */
  public Class<?> getType()
  {
    return Class.class;
  }



  /**
   * Converts an object to a string representation. This basic implementation
   * returns object.toString() or an empty string if the given object is null.
   * 
   * @param object
   *          the object to be converted, possibly null
   * @param type
   *          the property class of the object, not null
   * @param context
   *          the context, not null
   * @return a String representation, not null
   * @see org.apache.commons.betwixt.strategy.ObjectStringConverter#objectToString(Object,
   *      Class, Context)
   * @see com.genia.toolbox.basics.bean.RegisterableObjectStringConverter#objectToString(java.lang.Object,
   *      java.lang.Class, org.apache.commons.betwixt.expression.Context)
   */
  public String objectToString(Object object, Class<?> type, Context context)
  {
    return ((Class<?>) object).getName();
  }



  /**
   * Converts a string representation to an object. It is acceptable for an
   * implementation to return the string if it cannot convert the string to the
   * given class type. This basic implementation just returns a string.
   * 
   * @param value
   *          the String to be converted
   * @param type
   *          the property class to be returned (if possible), not null
   * @param context
   *          the context, not null
   * @return an Object converted from the String, not null
   * @see org.apache.commons.betwixt.strategy.ObjectStringConverter#stringToObject(String,
   *      Class, Context)
   * @see com.genia.toolbox.basics.bean.RegisterableObjectStringConverter#stringToObject(java.lang.String,
   *      java.lang.Class, org.apache.commons.betwixt.expression.Context)
   */
  public Object stringToObject(String value, Class<?> type, Context context)
  {
    try {
      return Class.forName(StringUtils.trimWhitespace(value));
    }
    catch (ClassNotFoundException e) {
      throw new WrapperException(e);
    }
  }

}
